package com.leyou.item.web;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/spec")
public class SpecController {

    private SpecGroupService groupService;
    private SpecParamService paramService;

    public SpecController(SpecGroupService groupService, SpecParamService paramService) {
        this.groupService = groupService;
        this.paramService = paramService;
    }

    /**
     * 根据商品分类id，查询规格组的集合
     * @param id
     * @return
     */
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> listSpecGroupByCategoryId(@RequestParam("id") Long id){
        return ResponseEntity.ok(groupService.listSpecGroupByCategoryId(id));
    }

    /**
     * 根据条件查询规格参数集合
     * @param cid 商品分类id
     * @param gid 规格组id
     * @param isSearching 是否搜索
     * @return
     */
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> ListSpecParamsByCondition(
          @RequestParam(value = "categoryId",required = false)Long cid,
          @RequestParam(value = "groupId",required = false)Long gid,
          @RequestParam(value = "searching",required = false)Boolean isSearching
    ){
        return ResponseEntity.ok(paramService.ListSpecParamsByCondition(cid,gid,isSearching));
    }

    /**
     * 新增规格租
     * @param specGroupDTO 要新增的规格租
     * @return
     */
    @PostMapping("/group")
    public ResponseEntity<Void> saveSpecGroup(@RequestBody SpecGroupDTO specGroupDTO){
        groupService.saveSpecGroup(specGroupDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改规格组
     * @param specGroupDTO 要修改的規格組信息
     * @return
     */
    @PutMapping("/group")
    public ResponseEntity<Void> updateSpecGroup(@RequestBody SpecGroupDTO specGroupDTO){
        groupService.updateSpecGroup(specGroupDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 新增规格参数
     * @param specParamDTO 要新增的规格参数
     * @return
     */
    @PostMapping("/param")
    public ResponseEntity saveSpecParam(@RequestBody SpecParamDTO specParamDTO){
        paramService.saveSpecParam(specParamDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改规格参数
     * @param specParamDTO 要修改的规格参数
     * @return
     */
    @PutMapping("/param")
    public ResponseEntity<Void> updateSpecParam(@RequestBody SpecParamDTO specParamDTO){
        paramService.updateSpecParam(specParamDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     *根据分类id查询规格组及组内参数
     * @param cid 分类id
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> listGroupAndParamByCid(@RequestParam("id")Long cid){
        return ResponseEntity.ok(groupService.listGroupAndParamByCid(cid));
    }

}